package org.item.user.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.item.common.bean.SysRole;
import org.item.common.bean.SysUser;
import org.item.common.enumbean.RoleStatus;
import org.item.common.enumbean.UserStatus;
import org.item.common.service.IRoleService;
import org.item.user.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleService implements IRoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public SysRole insert(SysRole role) {
        roleMapper.insert(role);
        SysRole result=this.getById(role.getId());
        return result;
    }

    @Override
    public SysRole update(SysRole role) {
        roleMapper.updateById(role);
        SysRole result=this.getById(role.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int num=roleMapper.deleteById(id);
        return num;

    }

    @Override
    public SysRole getById(Integer id) {
        SysRole result=roleMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<SysRole> getListPage(IPage page, SysRole role) {
        Wrapper wrapper=new QueryWrapper(role);
        IPage result=roleMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public List<SysRole> getList(SysRole role) {
        Wrapper wrapper=new QueryWrapper(role);
        List<SysRole> result=roleMapper.selectList(wrapper);
        return result;
    }

    @Override
    public int setStatus(Integer id) {
        UpdateWrapper wrapper=new UpdateWrapper();
        SysRole sysRole=roleMapper.selectById(id);
        if (sysRole.getStatus()== RoleStatus.START){
            wrapper.set("status",RoleStatus.STOP);
        }else {
            wrapper.set("status",RoleStatus.START);
        }
        wrapper.eq("id",id);
        int result=roleMapper.update(wrapper);
        return result;
    }

    @Override
    public SysRole assignPermission(SysRole role) {
        return null;
    }
}
